﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Extract HTML content</title>
    <link href="styles/js-console.css" rel="stylesheet" />
    <script src="scripts/string-extensions.js"></script>
</head>
<body>
    <div id="js-console"></div>
    <script src="scripts/js-console.js"></script>
    <script>

        jsConsole.writeLine("That's how it appears with the tags!");
        jsConsole.writeLine("---------------------------------------------------");
        jsConsole.writeLine("<html><head><title>Sample site" +
            "</title></head><body><div>text<div>more text</div>and more...</div>in body</body></html>");
        jsConsole.writeLine();
        var text = "<html><head><title>Sample site" +
            "</title></head><body><div>text<div>more text</div>and more...</div>in body</body></html>";
        jsConsole.writeLine("---------------------------------------------------");
        jsConsole.writeLine("That's how it appears when the content of the tags is extracted !");
        jsConsole.writeLine();
        jsConsole.writeLine(extractContent(text));
        jsConsole.writeLine("---------------------------------------------------");
        function extractContent(text) {
            var extractedText = buildStringBuilder();
            var indexRigthBraket = text.indexOf(">");
            while (indexRigthBraket > -1 && indexRigthBraket != text.length - 1) {
                if (text[indexRigthBraket + 1] != "<") {
                    var stringStart = text.substring(indexRigthBraket + 1, text.indexOf("<", indexRigthBraket + 1));
                    // var stringEnd = text.indexOf("<", indexRigthBraket + 1);
                    extractedText.append(stringStart).append(" ");
                }
                var indexRigthBraket = text.indexOf(">", indexRigthBraket + 1);
            }
            return extractedText.toString();
        }
        function buildStringBuilder() {
            return {
                strs: [],
                len: 0,
                append: function (str) {
                    this.strs[this.len++] = str;
                    return this;
                },
                toString: function () {
                    return this.strs.join("");
                }
            };
        }



    </script>

</body>
</html>